package fun.tan90.solon.web;

import com.mybatisflex.annotation.UseDataSource;
import fun.tan90.solon.entity.dataobject.BinlogMaster;
import fun.tan90.solon.mapper.db1.BinlogMasterMapper;
import lombok.extern.slf4j.Slf4j;
import org.noear.redisx.RedisClient;
import org.noear.solon.annotation.*;
import org.noear.solon.cache.jedis.RedisCacheService;
import org.noear.solon.core.event.EventBus;

import java.util.List;

@Slf4j
@Controller
public class DemoController {

    @Init
    public void init() {
        System.out.println("DemoController init");
    }

    @Destroy
    public void destroy() {
        System.out.println("DemoController destroy");
    }

    @Mapping("/hello")
    public String hello(@Param(defaultValue = "world") String name) {
        EventBus.subscribe(String.class, event -> {
            System.out.println(event);
        });
        EventBus.publish("hello chj");
        log.info("hello chj");
        return String.format("Hello %s!", name);
    }

    @Inject
    BinlogMasterMapper binlogMasterMapper;

    @Mapping("/db1")
    @UseDataSource("db1")
    public List<BinlogMaster> db1() {
        return binlogMasterMapper.selectAll();
    }

    @Inject
    RedisCacheService redisCacheService;

    @Mapping("/cache")
    public String cache() {
        RedisClient redisClient = redisCacheService.client();
        redisClient.open(session -> {
            session.key("key1").set("val1");
        });
        return redisClient.openAndGet(session -> session.key("key1").get());
    }
}